package org.kms.Controllers;

import org.kms.dao.garden_listDao;
import org.kms.pojo.Garden_list;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Graden")
@CrossOrigin(value = "*")
public class graden {

    @Autowired
    private garden_listDao grade_listDao;

    @RequestMapping(value = "/index")
    @ResponseBody
    public ResponseResult<List<Garden_list>> getall()throws Exception{
        return new ResponseResult<>(grade_listDao.getAll());
    }


    @RequestMapping(value = "/add")
    @ResponseBody
    public ResponseResult add(Garden_list garden_list)throws Exception{
        grade_listDao.add(garden_list);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public ResponseResult update(Garden_list garden_list)throws Exception{
        grade_listDao.update(garden_list);
        return ResponseResult.createSuccessResult();
    }

    @InitBinder
    public void InitBinder(WebDataBinder webDataBinder) throws Exception{
        //日期类型转换器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat,true);
        //注册类型换换器:如果碰到Date.class 都会转成yyyy-MM-dd格式
        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }
}
